package com.yuanqi.Service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanqi.Common.Expriess.BaseException;
import com.yuanqi.Common.config.ThreadLocalConfig;
import com.yuanqi.Mapper.FollowMapper;
import com.yuanqi.Mapper.UserMapper;
import com.yuanqi.Common.Pojo.Vo.FollowVo;
import com.yuanqi.Common.Pojo.entity.Follow;
import com.yuanqi.Common.Pojo.entity.User;
import com.yuanqi.Service.FollowService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {
    @Resource
    FollowMapper followMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    ThreadLocalConfig threadLocalConfig;

    /**
     * 关注
     *
     * @param follow
     */
    @Override
    public void addFollow(Follow follow) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, follow.getFollowId()));
        if (ObjectUtil.isEmpty(user)) {
            throw new BaseException("用户不存在");
        }
        Long userId = Long.valueOf(threadLocalConfig.get().get("userId").toString());
        follow.setUserId(userId);
        follow.setCreateTime(System.currentTimeMillis() / 1000);
        followMapper.insert(follow);
    }

    @Override
    public void cancelFollow(Follow follow) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, follow.getFollowId()));
        if (ObjectUtil.isEmpty(user)) {
            throw new BaseException("用户不存在");
        }
        Long userId = Long.valueOf(threadLocalConfig.get().get("userId").toString());

        followMapper.delete(new LambdaQueryWrapper<Follow>().eq(Follow::getFollowId, follow.getFollowId()).eq(Follow::getUserId, userId));
    }

    /**
     * 互相关注
     *
     * @param current
     * @return
     */
    @Override
    public List<FollowVo> eachOther(Integer current) {
        Long userId = Long.valueOf(threadLocalConfig.get().get("userId").toString());
        Integer start = (current - 1) * 10;
        List<FollowVo> list = followMapper.eachOther(userId, start);
        if (!list.isEmpty()) {
            list.forEach(v -> {
                v.setIsFollow(2);
            });
        }

        return list;
    }

    /**
     * 获取粉丝
     *
     * @param current
     * @return
     */
    @Override
    public List<FollowVo> followers(Integer current) {
        Long userId = Long.valueOf(threadLocalConfig.get().get("userId").toString());
        Integer start = (current - 1) * 10;
        List<FollowVo> followerslist = followMapper.flowers(userId, start);
        List<FollowVo> eachOtherlist = followMapper.eachOther(userId, start);
        for (FollowVo followVo : followerslist) {
            followVo.setIsFollow(3);
            if (ObjectUtil.isNotEmpty(eachOtherlist)) {
                for (FollowVo followVo1 : eachOtherlist) {
                    if (followVo1.getFollowId().equals(followVo.getFollowId())) {
                        followVo.setIsFollow(2);
                    }
                }
            }

        }
        return followerslist;
    }

    /**
     * 关注列表
     *
     * @param current
     * @return
     */
    @Override
    public List<FollowVo> concern(Integer current) {
        Long userId = Long.valueOf(threadLocalConfig.get().get("userId").toString());
        Integer start = (current - 1) * 10;
        List<FollowVo> concernList = followMapper.concern(userId, start);
        List<FollowVo> eachOtherlist = followMapper.eachOther(userId, start);
        for (FollowVo followVo : concernList) {
            followVo.setIsFollow(1);
            if (ObjectUtil.isNotEmpty(eachOtherlist)) {
                for (FollowVo followVo1 : eachOtherlist) {
                    if (followVo.getFollowId().equals(followVo1.getFollowId())) {
                        followVo.setIsFollow(2);
                    }
                }
            }

        }
        return concernList;
    }

    @Override
    public Integer isFollow(Long userId, Long followId) {
        LambdaQueryWrapper<Follow> wrapper = new LambdaQueryWrapper<Follow>().eq(Follow::getUserId, userId).eq(Follow::getFollowId, followId);
        Follow follow = followMapper.selectOne(wrapper);
        LambdaQueryWrapper<Follow> wrapper1 = new LambdaQueryWrapper<Follow>().eq(Follow::getFollowId, userId).eq(Follow::getUserId, followId);
        Follow follow1 = followMapper.selectOne(wrapper1);
        Integer isfollow = 0;
        if (ObjectUtil.isEmpty(follow) && ObjectUtil.isEmpty(follow1)) {
            isfollow = 0;
        } else if (ObjectUtil.isNotEmpty(follow) && ObjectUtil.isEmpty(follow1)) {
            isfollow = 1;
        } else if (ObjectUtil.isEmpty(follow) && ObjectUtil.isNotEmpty(follow1)) {
            isfollow = 3;
        } else {
            isfollow = 2;
        }
        return isfollow;
    }


}
